<template>
  <view>
    <my-search :radius="25" :bgColor="'#c00000'" @click="toSearch()"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧一级导航行-->
      <scroll-view class="scroll-left" :style="{height:SysHeight + 'px'}" scroll-y>
        <block v-for="(item,index) in cateList" :key="index">
          <view :class="['scroll-left-item', active == index ? 'active':' ' ]" @click="acticeChange(index)">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧二级导航行-->
      <scroll-view class="scroll-right" :style="{height:SysHeight + 'px'}" scroll-y :scroll-top="scrollTop">
        <view class="scroll-right-item" v-for="(item2,index) in cateLevel2" :key="index">
          <!-- 二级标题 -->
          <text>/ {{item2.cat_name}} /</text>
          <!-- 三级渲染区域-->
          <view class="scroll-right-level3" style="display: flex;flex-wrap: wrap;margin-top: 10px;">
            <view class="scroll-right-item-child" v-for="(item3,index) in item2.children" :key="index"
              @click="todoGoodsList(item3)">
              <!-- 三级图片-->
              <image :src="substr(item3.cat_icon)"></image>
              <!-- 三级标题-->
              <text class="title">{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'

  export default {
    mixins: [badgeMix],
    data() {
      return {
        SysHeight: 0,
        cateList: [], //一级列表数据
        active: 0,
        cateLevel2: [], //二级列表数据
        scrollTop: 0
      };
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.SysHeight = sysInfo.windowHeight - 50
      //获取左侧一级列表数据
      this.getCateList()
    },
    methods: {
      //原来接口下的图片路径域名过期，把域名换走，换成一个可访问的
      substr(str) {
        //旧的访问不了的
        let s = "https://api-ugo-dev.itheima.net/";
        //截掉这个访问不了的，换成一个新的
        str = str.substring(s.length, str.length);
        //这个就是新的
        str = "https://api-hmugo-web.itheima.net/" + str;
        return str;
      },

      //获取左侧一级列表数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        //获取二级列表数据
        this.cateLevel2 = res.message[0].children
      },

      //修改active
      acticeChange(index) {
        //修改active
        this.active = index
        //动态获取二级列表数据
        this.cateLevel2 = this.cateList[index].children
        //修改scrollTop，他的值不能一直是0，原来是0，现在也不能是0，所以要动态赋值
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },

      //三级数据跳转商品详情页面
      todoGoodsList(item3) {
        uni.navigateTo({
          url: '/subpackages/goods_list/goods_list?cid=' + item3.cat_id
        })
      },

      //前往搜索
      toSearch() {
        uni.navigateTo({
          url: '/subpackages/search/search'
        })
      }

    },


  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .scroll-left {
      width: 120px;

      .scroll-left-item {
        background-color: #f7f7f7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: red;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }

    .scroll-right {
      .scroll-right-item {
        padding: 15px 0;
        text-align: center;
        font-weight: bold;
        font-size: 15px;

        .scroll-right-item-child {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          margin-bottom: 10px;

          image {
            width: 60px;
            height: 60px;
          }

          .title {
            font-size: 12px;
          }
        }
      }
    }
  }
</style>
